<template>
  <div class="pet-view">
    <PetList 
      v-if="!showShop"
      :pets="pets"
      @show-shop="showShop = true"
      @select-pet="selectPet"
      @feed-pet="feedPet"
      @play-with-pet="playWithPet"
      @train-pet="trainPet"
    />
    
    <PetShop
      v-else
      @adopt-pet="adoptPet"
      @close="showShop = false"
    />
  </div>
</template>

<script>
import PetList from '@/components/pet/PetList.vue';
import PetShop from '@/components/pet/PetShop.vue';

export default {
  name: 'PetView',
  
  components: {
    PetList,
    PetShop
  },
  
  data() {
    return {
      showShop: false,
      pets: [],
      selectedPetId: null
    }
  },
  
  created() {
    // 从本地存储加载宠物数据
    const savedPets = localStorage.getItem('pets');
    if (savedPets) {
      this.pets = JSON.parse(savedPets);
    }
  },
  
  methods: {
    savePets() {
      localStorage.setItem('pets', JSON.stringify(this.pets));
    },
    
    adoptPet(pet) {
      this.pets.push(pet);
      this.savePets();
      this.showShop = false;
    },
    
    selectPet(pet) {
      this.selectedPetId = pet.id;
    },
    
    feedPet(petId) {
      const pet = this.pets.find(p => p.id === petId);
      if (!pet) return;
      
      pet.hunger = Math.min(100, pet.hunger + 20);
      pet.happiness = Math.min(100, pet.happiness + 5);
      pet.lastFed = new Date();
      
      this.savePets();
    },
    
    playWithPet(petId) {
      const pet = this.pets.find(p => p.id === petId);
      if (!pet) return;
      
      pet.happiness = Math.min(100, pet.happiness + 15);
      pet.hunger = Math.max(0, pet.hunger - 5);
      pet.experience += 10;
      pet.lastPlayed = new Date();
      
      // 检查升级
      if (pet.experience >= pet.level * 100) {
        pet.level += 1;
        pet.experience = 0;
        pet.health = 100;
        pet.happiness = 100;
      }
      
      this.savePets();
    },
    
    trainPet(petId) {
      const pet = this.pets.find(p => p.id === petId);
      if (!pet) return;
      
      // 随机选择一个技能进行训练
      const skillTypes = [
        {
          id: 'harvest',
          name: '收获助手',
          icon: '🌾',
          description: '帮助收获作物，提高收获效率'
        },
        {
          id: 'guard',
          name: '农场守卫',
          icon: '🛡️',
          description: '保护作物免受害虫侵扰'
        },
        {
          id: 'water',
          name: '浇水能手',
          icon: '💧',
          description: '帮助浇水，提高作物生长速度'
        }
      ];
      
      let skill = pet.skills.find(s => s.id === skillTypes[0].id);
      if (!skill) {
        skill = {
          ...skillTypes[0],
          level: 1,
          experience: 0
        };
        pet.skills.push(skill);
      }
      
      skill.experience += 20;
      
      if (skill.experience >= skill.level * 50) {
        skill.level += 1;
        skill.experience = 0;
      }
      
      pet.happiness = Math.max(0, pet.happiness - 10);
      pet.experience += 15;
      pet.lastTrained = new Date();
      
      this.savePets();
    }
  }
}
</script>

<style scoped>
.pet-view {
  min-height: 100vh;
  background: #f5f5f5;
}
</style> 